import copy
import time

import pygame
import sys
import random
import os
from game.snake.entity.block import Block
from game.snake.utils.block_action import BlockAction
from game.snake.utils.utils import Utils

pygame.init()

# 移动步长
step = 50
width_screen = 20
height_screen = 15
# 设置窗口大小和标题
screen = pygame.display.set_mode((step*width_screen, step*height_screen))
pygame.display.set_caption("小蜜蜂吃花朵小游戏")
# 背景图片
background_image_path = "img_sky.png"
# 方块图片
block_img_bee_path = "bee.png"
block_img_flower_path = "flower.png"

# 设置背景颜色为淡蓝色
bg_color = (155,196,226)
red = (255,0,0)
# 方块集合
block_bee_group = []
# 排除数字结合
exclude_nums = [1,2]
# 移动速度
speed = 0.1
# 分数
score = 0

# 上一次 末尾位置
block_last = [Block(0,0,step,step,(255,255,255))]
# 运行状态
running = True
zanting = False
# 文件路径
main_dir = os.path.split(os.path.abspath(__file__))[0]

# 设置得分
score = 0
# 创建字体对象并设置字体和字号
font = pygame.font.Font(None, 36)
# 在屏幕右上角显示得分
score_color = (255, 255, 255)
score_text = font.render("Score: " + str(score), True, score_color)
screen.blit(score_text, (10, 10))
#加载图片
block_img_bee_new = Utils.load_image(block_img_bee_path,(step,step),main_dir)
block_img_flower_new = Utils.load_image(block_img_flower_path,(step,step),main_dir)

# 重新开始
def restart_game():
    global running,block_bee_group,exclude_nums,block_new,score,block_flower_group
    block_bee_group = []
    exclude_nums = []
    block_flower_group = []
    block_0 = Block(300,400,step,step,(255,255,255))
    # block_0.setText(pygame,"1")
    # block_img_new = Utils.load_image(block_img_bee_path,block_0.size(),main_dir)
    block_0.setImage(block_img_bee_new)
    block_bee_group.append(block_0)

    block_new = newBlock()
    # block_new.setText(pygame,"2")
    block_flower_group.append(block_new)

    block_last = []
    score = 0
    return block_bee_group

def newBlock():
    block_new_new = Block(0,0,step,step,(255,255,255))
    block_new_new.x = abs(step * (random.randint(1, width_screen-1)))
    block_new_new.y = abs(step * (random.randint(1, height_screen-1)))
    block_new_new.x = abs(block_new_new.x)
    block_new_new.y = abs(block_new_new.y)
    block_new_new.color = (random.randint(0, 255),random.randint(0, 255),random.randint(0, 255))
    #获取随机数字+字母
    random_num = Utils.getRandom(exclude_nums)
    # block_new_new.setText(pygame,str(chr(random_num)))
    # block_img_new = Utils.load_image(block_img_flower_path,block_new_new.size(),main_dir)
    block_new_new.setImage(block_img_flower_new)
    return block_new_new

def game_loop():
    global running,block_bee_group,exclude_nums,block_new,time_start,main_dir,block_last,block_flower_group,score,zanting
    # 开始时间
    time_start = time.time()
    bg_music = Utils.load_sound("house_lo.wav",main_dir)
    bg_music.play(-1)

    block_0 = Block(300,300,step,step,(255,255,255))
    # block_0.setText(pygame,"1")
    block_img = Utils.load_image(block_img_bee_path,block_0.size(),main_dir)
    block_0.setImage(block_img)
    block_bee_group.append(block_0)
    if len(block_bee_group)==1:
        block_last[0] = copy.copy(block_bee_group[len(block_bee_group)-1])
        block_last[0].x+= block_bee_group[0].width
        block_flower_group = []
        block_flower_group.append(newBlock())
    # 游戏循环
    while running:
        # 处理游戏事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                # 原地掉头
                print("按空格键了")
                BlockAction.drawAll(block_bee_group,-1,screen)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                print("鼠标单机")
                if event.button == 1:
                    print("鼠标单机重新开始")
                    block_bee_group = restart_game()
                    zanting = False

            # 更新小蜜蜂的位置
            keys = pygame.key.get_pressed()
            if keys[pygame.K_LEFT]:
                if block_bee_group[0].x -step >=0:
                    BlockAction.moveAll(-step,0,block_bee_group,"left",block_last)
            if keys[pygame.K_RIGHT]:
                if block_bee_group[0].x +step < screen.get_width():
                    BlockAction.moveAll(step,0,block_bee_group,"right",block_last)
            if keys[pygame.K_UP]:
                if block_bee_group[0].y -step >=0:
                    BlockAction.moveAll(0,-step,block_bee_group,"up",block_last)
            if keys[pygame.K_DOWN]:
                if block_bee_group[0].y + step < screen.get_height():
                    BlockAction.moveAll(0,step,block_bee_group,"down",block_last)

        if zanting:
            continue
        if len(block_flower_group)>10:
            zanting = True
        # 碰撞处理
        # for block_new_temp in block_flower_group:
        #     if pygame.Rect(block_bee_group[0].show()).colliderect(pygame.Rect(block_new_temp.show())):
        #         file = "boom.wav"
        #         boom = Utils.load_sound(file,main_dir)
        #         boom.play()
        #         print("碰到一起了",block_new_temp.size())
        #         block_temp = copy.copy(block_new_temp)
        #         block_temp.x = block_last[0].x
        #         block_temp.y = block_last[0].y
        #         block_temp.image = None
        #         # block_temp.color = (255,255,255)
        #         # block_bee_group.append(block_temp)
        #         block_new_temp = newBlock()
        #         block_flower_group.append(block_new_temp)
        # 碰撞
        block_score = [ x for x in block_flower_group if pygame.Rect(block_bee_group[0].show()).colliderect(pygame.Rect(x.show())) ]
        score+= len(block_score)
        if len(block_score)>0:
            file = "boom.wav"
            boom = Utils.load_sound(file,main_dir)
            boom.play()
        score_text = font.render("Score: " + str(score), True, score_color)

        block_flower_group = [ x for x in block_flower_group if pygame.Rect(block_bee_group[0].show()).colliderect(pygame.Rect(x.show())) == False  ]
        # 绘制背景
        background_image = Utils.load_image(background_image_path,screen.get_size(),main_dir)
        background = pygame.Surface(screen.get_size())
        for x in range(0,screen.get_width(),background_image.get_width()):
            background.blit(background_image,(x,0))
            for y in range(0,screen.get_height(),background_image.get_height()):
                background.blit(background_image,(x,y))
        #设置背景颜色
        screen.fill(bg_color)
        # 设置背景图片
        screen.blit(background,(0,0))
        # 绘制方块
        BlockAction.drawAll(block_bee_group,1,screen)
        # 方块图片
        # block_img = Utils.load_image(block_img_path,block_new.size(),main_dir)
        # block_new.setImage(block_img)
        # 自动移动
        for block_new_tem in block_flower_group:
            BlockAction.moveAuto(BlockAction,block_new_tem,speed,screen)
        # block_new.draw(screen,pygame)
        BlockAction.drawAll(block_flower_group,1,screen)
        screen.blit(score_text, (10, 10))
        # 更新屏幕显示
        pygame.display.update()
        time_now = time.time()
        if time_now-time_start> 1:
            time_start = time_now
            # 新增一个
            block_flower_group.append(newBlock())


    # 退出游戏
    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    game_loop()
